package day01;

import java.util.Random;
import java.util.Scanner;

/**生成验证码并与输入的验证码相比较*/
public class Demo04 {
    public static void main(String[] args) {
        String str = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int index = str.length();
        StringBuilder strb = new StringBuilder(4);
        Random random = new Random();
        strb.append(str.charAt(random.nextInt(index)))   //4次随机数，取随机数下标的字符放入字符串中进行扩容
                .append(str.charAt(random.nextInt(index)))
                .append(str.charAt(random.nextInt(index)))
                .append(str.charAt(random.nextInt(index)));
        String str2 = strb.toString();
        System.out.println("验证码为："+strb);
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入验证码：");
        String str3 = scan.nextLine();
        if (str2.equalsIgnoreCase(str3)){
            System.out.println("您输入的验证正确!");
        }else{
            System.out.println("您输入的验证错误!");
        }


    }
}
